package model.client;

import model.client.responsehandler.CreateGroupResponseHandler;
import model.client.responsehandler.SendToIndividualsResponseHandler;
import model.codec.PacketDecoder;
import model.codec.PacketEncoder;
import model.client.responsehandler.AllIdResponseHandler;
import model.client.responsehandler.ClientChatMessageHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

/**
 * @Description 客户端初始化类，为客户端添加对应的Handler
 * @author czc
 */
public class ClientInitializer extends ChannelInitializer<NioSocketChannel> {
    @Override
    protected void initChannel(NioSocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,
                7,
                4));
        //pipeline.addLast(new Spliter());
        pipeline.addLast(new PacketDecoder());
        pipeline.addLast(new AllIdResponseHandler());
        pipeline.addLast(new CreateGroupResponseHandler());
        pipeline.addLast(new SendToIndividualsResponseHandler());
        pipeline.addLast(new ClientChatMessageHandler());
        pipeline.addLast(new PacketEncoder());



//        pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE,
//                ClassResolvers.cacheDisabled(null)));
//
////        pipeline.addLast(new IdleStateHandler(10,
////                10,
////                20,
////                TimeUnit.MINUTES));
//        pipeline.addLast(new AllIdResponseHandler());
//        pipeline.addLast(new ObjectEncoder());
    }
}
